<?php

class OOWP_Base
{

    protected $dict = array( );

    public function __construct(array $args = array( ))
    {
        foreach ($args as $key => $value) {
            try {
                $this->$key = $value;
            } catch (Exception $e) {
                print $e;
            }
        }
    }

    /* OVERRIDES */

    public function __get($name)
    {
        $method = 'get' . $this->convert_underscores($name);
        if (method_exists($this, $method)) {
            return $this->$method( );
        } else {
            throw new Exception('Cannot get attribute "' . $name . '" in ' . get_class($this) . '.');
        }
    } // __get

    public function __set($name, $value)
    {
        if (defined('OOWP_DEBUG') && OOWP_DEBUG) {
            echo '<p>' . get_class($this) . ': ' . $name . '</p>';
        }
        $method = 'set' . $this->convert_underscores($name);
        if (method_exists($this, $method)) {
            $this->$method($value);
        } else {
            throw new Exception('Cannot set attribute "' . $name . '" in ' . get_class($this) . '.');
        }
    } // __set

    public function __toString( ) {
        if (method_exists($this, 'getFormatString')) {
            return get_class($this) . '(' . $this->format_string . ')';
        } else {
            return get_class($this) . '( )';
        }
    } // __toString

    /* METHODS */

    public static function convert_underscores($name)
    {
        return implode('', array_map("ucfirst", explode('_', $name)));
    } // convert_underscores

    public static function is_wellformed_string($string)
    {
        if (is_string($string)) {
            if (preg_match('/-?[_a-zA-Z]+[_a-zA-Z0-9-]*/', $string) == 1) {
                return true;
            }
        }
        return false;
    } // is_wellformed_string

    public static function is_dict_of_wellformed_strings(array $strings)
    {
        foreach ($strings as $key => $value) {
            if (!self::is_wellformed_string($key)) {
                return false;
            }
            if (is_array($value)) {
                if (!self::is_list_of_wellformed_strings($value)) {
                    return false;
                }
            } else {
                if (!self::is_wellformed_string($value)) {
                    return false;
                }
            }
        }
        return true;
    } // isdict_of_wellformed_strings

    public static function is_list_of_wellformed_strings(array $strings)
    {
        foreach ($strings as $string) {
            if (!self::is_wellformed_string($string)) {
                return false;
            }
        }
        return true;
    } // is_list_of_wellformed_strings

    public static function all_keys_are_numeric(array $array)
    {
        if (count(array_filter(array_keys($array), 'is_numeric')) == count($array)) {
            return true;
        }
        return false;
    }

    public static function all_keys_are_strings(array $array)
    {
        if (count(array_filter(array_keys($array), 'is_string')) == count($array)) {
            return true;
        }
        return false;
    }

    public static function all_keys_in_one_array_are_keys_in_another(array $needles, array $haystack)
    {
        foreach (array_keys($needles) as $needle) {
            if (!in_array($needle, array_keys($haystack))) {
                return false;
            }
        }
        return true;
    }

}
